package com.artron.mediaartron.ui.fragment.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.KeyEvent;

import rx.functions.Action0;

import static android.content.ContentValues.TAG;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-11-01.
 */

public class ProgressDialogFragment extends DialogFragment {

    private String mMessage;

    private static int position = 1;

    public String getOurTag() {
        return "ProgressDialog" + position++;
    }

    public static ProgressDialogFragment newInstance(String message) {
        ProgressDialogFragment fragment = new ProgressDialogFragment();
        fragment.init(message);
        return fragment;
    }

    private Action0 mBackAction;

    public void setOnBackPressListener(Action0 action) {
        mBackAction = action;
    }

    private void init(String message) {
        mMessage = message;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog dialog = new ProgressDialog.Builder(getContext())
                .setMessage(mMessage)
                .setCancelable(false)
                .setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_BACK) {
                            if (mBackAction != null) {
                                mBackAction.call();
                            }
                        }
                        Log.d(TAG, "onKey: ");
                        return false;
                    }
                })
                .create();
        dialog.setCanceledOnTouchOutside(false);


        return dialog;
    }

}
